package dk.itu.iwjx.helper;

import java.sql.*;
import java.util.*;

import dk.itu.iwjx.beans.UserBean;

public class UserHelper {
	private PreparedStatement addRecord, getRecords;
	private UserBean user = null;

    public UserBean getUser(UserBean user) throws ClassNotFoundException{

	 try {
		getRecords = DBConnection.getConn()
		.prepareStatement("SELECT id, address, phone, zipcode FROM users WHERE name = ? AND password = ? ");
	  System.out.print(user.getName() + user.getPassword() + " UN");
	  
	 getRecords.setString(1, user.getName());
	 getRecords.setString(2, user.getPassword());
	 
     ResultSet result = getRecords.executeQuery();

	// get row data
	while (result.next()) {
		user.setId(result.getInt(1));
		user.setAddress(result.getString(2));
		user.setPhone(result.getInt(3));
		user.setZipcode(result.getInt(4));
	  return user;
	   }
	 } catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				getRecords.close();
			 } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			DBConnection.close();
		}
  return null;
 }

 public boolean save(UserBean user){

//		addRecords = DBConnection.get()
//		.prepareStatement("INSERT INTO USERS( id, address, phone, zipcode FROM users WHERE name = ? AND password = ? ");
//	  System.out.print(user.getName() + user.getPassword() + " UN");
//	  
//	 addRecords.setString(1, user.getName());
//	addRecords.setString(2, user.getPassword());
//	 
//     ResultSet result = getRecords.executeQuery();
//
//	// get row data
//	while (result.next()) {
//		user.setId(result.getInt(1));
//		user.setAddress(result.getString(2));
//		user.setPhone(result.getInt(3));
//		user.setZipcode(result.getInt(4));
//	  return user;
//	   }
//	 } catch (SQLException e) {
//			e.printStackTrace();
//		}finally{
//			try {
//				getRecords.close();
//			 } catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			DBConnection.close();
//		}
//	 
//	 
	 return false;
 }
}
